package com.lmaye.design.patterns.structural.proxy.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * -- ProxyFactory
 *
 * @author lmay.Zhou
 * @date 2020/11/18 0:08 星期三
 * @qq 379839355
 * @email lmay@lmaye.com
 * @since JDK1.8
 */
public class ProxyFactory {
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("----- 动态代理 -----");
                        return method.invoke(target, args);
                    }
                });
    }
}
